<?php

class Timesheet extends CI_Controller {

	function __construct() {
		
		parent::__construct();
		
		echo is_null($this->session->userdata('email'));
		if ( !$this->session->userdata('email') ) { redirect('login'); }
		
		$this->load->model('Timesheet_model');
		$this->load->helper('timesheet_helper');
		
		$this->load->vars(array(
			'kanji_days'=>array('Monday'=>'æœˆ','Tuesday'=>'ç�«','Wednesday'=>'æ°´','Thursday'=>'æœ¨','Friday'=>'é‡‘','Saturday'=>'åœŸ','Sunday'=>'æ—¥')
		));
		
	}
	
	function index() {
		$data = $this->prepare_timesheet_data(array('date'=>$this->session->userdata('timesheet_date')))
			+ $this->prepare_timesheet_input_data(array('date'=>$this->session->userdata('timesheet_date')));
		$this->load->view('timesheet_view', $data);
	}
	
	function weekly() {
		$date_range = $this->get_start_and_end_of_week($this->session->userdata('timesheet_date'));

		$timesheet_data = $this->prepare_timesheet_data(array('from'=>$date_range['from'],'to'=>$date_range['to']));
		$timesheet_weekly_data = $this->prepare_timesheet_summary_data($timesheet_data['timesheet'], $timesheet_data['timesheet_info'],'weekly');	
		$timesheet_review_commit_data = $this->prepare_timesheet_review_commit_data(array('from'=>$date_range['from'],'to'=>$date_range['to']));

		$data = array_merge($timesheet_data,$timesheet_weekly_data,$timesheet_review_commit_data,$date_range);
		
		$this->load->view('timesheet_weekly_view',$data);
	}
	
	function print_out($from, $to) {
		if (!strtotime($from) || !strtotime($to)) { redirect('timesheet'); }
		
		$date_range = array('from'=>$from,'to'=>$to);

		$timesheet_data = $this->prepare_timesheet_data(array('from'=>$date_range['from'],'to'=>$date_range['to']));
		$timesheet_print_data = $this->prepare_timesheet_summary_data($timesheet_data['timesheet'], $timesheet_data['timesheet_info'],'print_out');	
		
		$data = array_merge($timesheet_data,$timesheet_print_data,$date_range);
		
		$this->load->view('timesheet_print_view',$data);
	}
	
	function prepare_timesheet_data($date_range) {
		$timesheet = $this->Timesheet_model->get_timesheet($this->session->userdata('email'), $date_range);
		$timesheet_info = $this->Timesheet_model->get_timesheet_record($this->session->userdata('email'), $date_range);
		return array('timesheet'=>$timesheet,'timesheet_info'=>$timesheet_info);
	}
	
	function prepare_timesheet_input_data($date_range) {
		$possible_activities_raw = $this->Timesheet_model->get_possible_activities($this->session->userdata('email'));
		$all_activities_raw = $this->Timesheet_model->get_all_activities();
		
		$projects = $work_order_nos = $activities = $activity_tree = array();
		foreach ($possible_activities_raw as $activity) {
			$projects[$activity->project_id] = $activity->project_name;
			$work_order_nos[$activity->project_id] = $activity->work_order_no;
			$activities[$activity->code] = $activity->code_name;
			$activity_tree[$activity->project_id][$activity->code][$activity->subcode] = $activity->subcode_name;
		}
		
		$indirect_activities = $indirect_activity_tree = array();
		foreach($all_activities_raw as $activity) {
			$indirect_activities[$activity->code] = $activity->code_name;
			$indirect_activity_tree[$activity->code][$activity->subcode] = $activity->subcode_name;
		}
		
		return array(
			'projects'=>$projects,
			'work_order_nos'=>$work_order_nos,
			'activities'=>$activities,
			'activity_tree'=>$activity_tree,
			'indirect_activities'=>$indirect_activities,
			'indirect_activity_tree'=>$indirect_activity_tree
		);
	}
	
	function prepare_timesheet_summary_data($timesheet, $timesheet_info, $format) {
		
		$date_format = ($format==='weekly') ? 'l' : ( ($format==='print_out') ? 'Y-m-d' : '' );
		
		$timesheet_activities = $timesheet_activity_tree = $timesheet_code_hours = $timesheet_subcode_hours = array();
		$timesheet_indirect_activities = $timesheet_indirect_activity_tree = $timesheet_indirect_code_hours = $timesheet_indirect_subcode_hours = array();
		$timesheet_projects = $timesheet_work_order_nos = array();
		$person_info = array(); //only for print_out

		foreach ($timesheet as $activity) {
			
			$timesheet_projects[$activity->project_id] = $activity->project_name;
			$timesheet_work_order_nos[$activity->project_id] = $activity->work_order_no;
			
			if ( $activity->project_id ) {
				$timesheet_activities[$activity->code] = $activity->code_name;
				$timesheet_activity_tree[$activity->project_id][$activity->code][$activity->subcode] = $activity->subcode_name;
				@$timesheet_code_hours[date($date_format,strtotime($activity->work_date))][$activity->project_id][$activity->code] += $activity->hours_spent;
				@$timesheet_subcode_hours[date($date_format,strtotime($activity->work_date))][$activity->project_id][$activity->code][$activity->subcode] += $activity->hours_spent;
			}
			else {
				$timesheet_indirect_activities[$activity->code] = $activity->code_name;
				$timesheet_indirect_activity_tree[$activity->code][$activity->subcode] = $activity->subcode_name;
				@$timesheet_indirect_code_hours[date($date_format,strtotime($activity->work_date))][$activity->code] += $activity->hours_spent;
				@$timesheet_indirect_subcode_hours[date($date_format,strtotime($activity->work_date))][$activity->code][$activity->subcode] += $activity->hours_spent;
			}

			foreach ($timesheet_info as $key => $activity) {
				$timesheet_info[$key]->work_date_day = date($date_format,strtotime($activity->work_date));
			}
		}
		
		if ($format==='print_out') {
			ksort($timesheet_code_hours);
			ksort($timesheet_subcode_hours);
			ksort($timesheet_indirect_code_hours);
			ksort($timesheet_indirect_subcode_hours);

			$this->load->model('Engineer_model');
			$this->load->model('Group_model');
			$person_info['group_name'] = $this->Group_model->get_group_name_of_person($this->session->userdata('email'));
			$person_info['employee_no'] = $this->Engineer_model->get_engineer_employee_number($this->session->userdata('email'));
		}
		
		return array(
			'timesheet_activities'=>$timesheet_activities,
			'timesheet_activity_tree'=>$timesheet_activity_tree,
			'timesheet_code_hours'=>$timesheet_code_hours,
			'timesheet_subcode_hours'=>$timesheet_subcode_hours,
			'timesheet_indirect_activities'=>$timesheet_indirect_activities,
			'timesheet_indirect_activity_tree'=>$timesheet_indirect_activity_tree,
			'timesheet_indirect_code_hours'=>$timesheet_indirect_code_hours,
			'timesheet_indirect_subcode_hours'=>$timesheet_indirect_subcode_hours,
			'timesheet_projects'=>$timesheet_projects,
			'timesheet_work_order_nos'=>$timesheet_work_order_nos,
			'timesheet_info'=>$timesheet_info,
			'date_format'=>$date_format,
			'format'=>$format,
			'person_info'=>$person_info
		);
	}
	
	function prepare_timesheet_review_commit_data($date_range) {
		$timesheet_review_status = $timesheet_commit_status = array();
		$review_commit_statuses = $this->Timesheet_model->get_timesheet_review_commit_statuses($this->session->userdata('email'), $date_range);
		foreach ($review_commit_statuses as $activity) {
			$timesheet_review_status[date('l',strtotime($activity->work_date))] = $activity->review_status;
			$timesheet_commit_status[date('l',strtotime($activity->work_date))] = $activity->commit_status;
		}
		return array(
			'timesheet_review_status'=>$timesheet_review_status,
			'timesheet_commit_status'=>$timesheet_commit_status
		);
	}
	
	function change_date() {
		$this->session->set_userdata( array('timesheet_date'=>$this->input->post('text_date')) );
		redirect('timesheet');
	}
	
	function change_week() {
		$this->session->set_userdata( array('timesheet_date'=>$this->input->post('text_date')) );
		redirect('timesheet/weekly');
	}
	
	function save() {
		
		$timesheet = $this->Timesheet_model->get_timesheet_record($this->session->userdata('email'), array('date'=>$this->session->userdata('timesheet_date')));
		$timesheet_exists = count($timesheet);
		$timesheet_id = NULL;
		$activities = array();
		
		foreach ($_POST as $key => $value) {
			if ( substr($key,0,strlen('activity_'))=='activity_' ) {
				$entry = explode('_',substr($key,strlen('activity_')));
				$activities['direct'][] = array('project_id'=>$entry[0],'code'=>$entry[1],'subcode'=>$entry[2],'hours_spent'=>floatval($value));
			} elseif (substr($key,0,strlen('indirect_activity_'))=='indirect_activity_' ) {
				$entry = explode('_',substr($key,strlen('indirect_activity_')));
				$activities['indirect'][] = array('code'=>$entry[0],'subcode'=>$entry[1],'hours_spent'=>floatval($value));
			}
		}
		
		if ($timesheet_exists) {
			$timesheet_id = $timesheet->timesheet_id;
			$this->Timesheet_model->update_timesheet($timesheet_id, floatval($this->input->post('text_regular_hours')), REVIEW_STATUS_SAVE_COMMIT, $this->input->post('submit_timesheet')==='Commit' ? 'true' : 'false');
			$timesheet_activities = $this->Timesheet_model->get_timesheet_activities($timesheet_id);
			if (count($timesheet_activities)>0) {
				$activity_ids = array();
				foreach($timesheet_activities as $timesheet_activity) {
					$timesheet_activity_ids[] = $timesheet_activity->activity_id;
				}
				$this->Timesheet_model->delete_timesheet_activities($timesheet_activity_ids);
			}
		} else {
			$timesheet_id = $this->Timesheet_model->create_timesheet($this->session->userdata('email'), $this->session->userdata('timesheet_date'), floatval($this->input->post('text_regular_hours')), REVIEW_STATUS_SAVE_COMMIT, $this->input->post('submit_timesheet')==='Commit' ? 'true' : 'false', date('Y-m-d'));
		}
		
		if (@count($activities['direct'])>0) { $this->Timesheet_model->create_activities($activities['direct'],$timesheet_id,'direct'); }
		if (@count($activities['indirect'])>0) { $this->Timesheet_model->create_activities($activities['indirect'],$timesheet_id,'indirect'); }
		
		redirect('timesheet');
	}
	
	function get_start_and_end_of_week($date) {
		$from_date = $date;
		if ( date('l',strtotime($date))!=START_OF_WEEK ) {
			$from_date = date('Y-m-d',strtotime('-1 '.START_OF_WEEK,strtotime($date)) );
		}
		$to_date = $date;
		if ( date('l',strtotime($date))!=END_OF_WEEK ) {
			$to_date = date('Y-m-d',strtotime('+1 '.END_OF_WEEK,strtotime($date)) );
		}
		return array('from'=>$from_date,'to'=>$to_date);
	}	

}


?>